﻿<!--
* 开源版本请务必保留此注释头信息，若删除捷码开源〔GEMOS〕官方保留所有法律责任追究！
* 本软件受国家版权局知识产权以及国家计算机软件著作权保护（登记号：2018SR503328）
* 不得恶意分享产品源代码、二次转售等，违者必究。
* Copyright (c) 2020 gemframework all rights reserved.
* http://www.gemframework.com
* 版权所有，侵权必究！
-->
<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="common/links :: common_header(~{::title},~{},~{})">
    <title>动态数据源表-模块</title>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">数据源名:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="请输入数据源名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">连接URL:</label>
                        <div class="layui-input-inline">
                            <input name="url" class="layui-input" placeholder="请输入连接信息"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button shiro:hasPermission="datasource:export" id="exportBtn" class="layui-btn icon-btn"
                                type="button">
                            <i class="layui-icon">&#xe67d;</i>导出
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a shiro:hasPermission="datasource:update" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="test">测试连接</a>
    <a shiro:hasPermission="datasource:update" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a shiro:hasPermission="datasource:delete" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">数据源名:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入数据源名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">驱动类型:</label>
            <div class="layui-input-block">
                <select id="dsDriverClassName"  name="driverClassName" lay-verType="tips" lay-verify="required" required>
                    <option value="com.mysql.jdbc.Driver">MySQL</option>
                    <option value="com.mysql.cj.jdbc.Driver">MySQL8</option>
                    <option value="net.sourceforge.jtds.jdbc.Driver">SQLServer</option>
                    <option value="oracle.jdbc.OracleDriver">Oracle</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">连接URL:</label>
            <div class="layui-input-block">
                <input name="url" placeholder="请输入连接信息" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">连接账号:</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入用户名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">连接密码:</label>
            <div class="layui-input-block">
                <input name="password" type="password" placeholder="请输入密码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<div th:replace="common/scripts :: common"></div>
<div th:replace="common/scripts :: jquery"></div>
<div th:replace="common/scripts :: custom"></div>
<div>
    <script>
        //全局变量 Shiro权限
        var shiroSave = false;
        var shiroDelete = false;
        var shiroUpdate = false;
        var shiroInfo = false;
    </script>
    <!-- 设置Shiro权限变量 -->
    <shiro:hasAllPermissions name="datasource:save">
        <script>shiroSave = true;</script>
    </shiro:hasAllPermissions>
    <shiro:hasAllPermissions name="datasource:delete">
        <script>shiroDelete = true;</script>
    </shiro:hasAllPermissions>
    <shiro:hasAllPermissions name="datasource:update">
        <script>shiroUpdate = true;</script>
    </shiro:hasAllPermissions>
</div>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var admin = layui.admin;

        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {field: 'name', title: '数据源名称', align: 'left'},
            {field: 'url', title: '连接URL', align: 'left'},
            {field: 'username', title: '帐号', align: 'left'},
            {field: 'password', title: '密码', templet: function (d) {
                    return "<span id='passwordText_"+d.id+"'><a lay-event=\"showPass\"  class=\"fa fa-eye-slash\" aria-hidden=\"true\"></a> | ***</span>"
                }, align: 'left'},
            {field: 'driverClassName', title: '数据库驱动类型', align: 'left'},
            {field: 'connStatus', title: '连接状态', templet: function (d) {
                    if(d.connStatus === 1){
                        return "<font color=\"green\">测试通过</font>";
                    }else if(d.connStatus === 2){
                        return "<font color=\"red\">连接失败</font>";

                    }else{
                        return "<font color=\"gray\">未知状态</font>";
                    }}, align: 'center'},
            {
                field: 'updateTime', title: '更新时间', templet: function (d) {
                    return util.toDateString(d.updateTime);
                }, sort: true
            },
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 200}
        ];

        var insTb = table.render({
            elem: '#dataTable',
            url: 'datasource/page',
            page: true,
            toolbar: [shiroToolbar(shiroSave, shiroDelete)].join(''),
            cellMinWidth: 100,
            cols: [cols]
        });

        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj.data);
            } else if (obj.event === 'test') { // 测试连接
                doTest(obj.data);
            } else if (obj.event === 'showPass') { // 查看密码
                $("#passwordText_"+obj.data.id+"").html("<a lay-event=\"hidePass\"  class=\"fa fa-eye\" aria-hidden=\"true\"></a> | "+obj.data.password)
            } else if (obj.event === 'hidePass') { // 查看密码
                $("#passwordText_"+obj.data.id+"").html("<a lay-event=\"showPass\"  class=\"fa fa-eye-slash\" aria-hidden=\"true\"></a> | ***")
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        // 显示编辑弹窗
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '800px',
                title: (mData ? '修改' : '添加'),
                content: $('#formDialog').html(),
                shadeClose:true,
                success: function (layero, dIndex) {
                    form.val('dataForm', mData);  // 回显数据
                    form.on('submit(saveBtn)', function (data) {
                        $.post(mData ? 'datasource/update' : 'datasource/save', data.field, function (res) {
                            if (res.code == 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });

        }

        /* 导出excel */
        $('#exportBtn').click(function () {
            var checkRows = table.checkStatus('dataTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '导出数据'
                });
            }
        });


        /* 删除 */
        window.doDel = function (obj) {
            if ([[${session.session_runtime}]] == 'demo') return layer.msg('演示环境不允许操作，请联系管理员', {icon: 4});
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('datasource/delete', {
                    id: obj ? obj.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 测试连接 */
        window.doTest = function (obj) {
            $.post('datasource/test', obj, function (res) {
                if (res.code === 0) {
                    layer.msg("恭喜，连接"+res.msg, {icon: 1});
                } else {
                    layer.msg("抱歉，连接"+res.msg, {icon: 2});
                }
                insTb.reload({page: {curr: 1}});
            }, 'json');
        }
    });
</script>
</body>
</html>